define([
  'vue',
  'text!' + ELMP.resource('components/vue-department/index.html', 'lib'),
], function (Vue, template) {
  return {
    template,
    name: 'SelectDepartment',
    props: {
      model: {
        type: Object
      },
      disabled: {
        type: Boolean,
        default: false
      },
      multiple: {
        type: Boolean,
        default: false
      },
    },
    model: {
      prop: 'model',
      event: 'renderDeartment'
    },
    watch: {
      model() {
        const name = this.model?.name
        this.setDeartData(name, !this.disabled)
      }
    },
    data() {
      return {
        props: {
          label: 'name',
          children: 'zones',
          isLeaf: 'isLeaf',
        },
        deartmentName: '',
        dialogVisible: false,
        searchDeartData: [],
        deartData: {},
        keyWord: "",
        canEidt: true,

      }
    },
    created() { },
    mounted() {
      $.el.util.writeCss(ELMP.resource('components/vue-department/index.css', 'lib'), '#deartment')
    },
    methods: {
      // 重置
      reset() {
        this.keyWord = ""
        $.el.get({
          url: ELMP.url(`/org/v1/org/get?parentId=-1`),
          success: (resp) => {
            this.searchDeartData = resp.res.data
          },
          error: (e) => {
            $.msg.error(e.message)
          },
        })
      },
      // 查询部门数据
      getDeart() {
        $.msg.loading('加载中')
        $.el.get({
          url: ELMP.url(`/org/v1/orgs`),
          success: (resp) => {
            if (this.keyWord && this.keyWord !== "") {
              this.searchDeartData = resp.res.data.filter(c => c.name.indexOf(this.keyWord) !== -1)
            } else {
              this.searchDeartData = []
            }
            $.msg.close()
          },
          error: (e) => {
            $.msg.close()
            return $.msg.error(e.message)
          },
        })
      },
      openDialog() {
        this.dialogVisible = true
      },
      loadNode(node, resolve) {
        if (node.level == 0) {
          this.loadfirstnode(resolve)
        }
        if (node.level >= 1) {
          this.loadchildnode(node, resolve)
        }
      },
      //加载第一级节点
      loadfirstnode(resolve) {
        $.el.get({
          url: ELMP.url(`/org/v1/org/get?parentId=-1`),
          success: function (resp) {
            return resolve(resp.res.data)
          },
          error: (e) => {
            return $.msg.error(e.message)
          },
        })
      },
      //加载节点的子节点集合
      loadchildnode(node, resolve) {
        $.el.get({
          url: ELMP.url(`/org/v1/org/get?parentId=${node.data.id}`),
          success: function (resp) {
            return resolve(resp.res.data)
          },
          error: (e) => {
            return $.msg.error(e.message)
          },
        })
      },
      // 关闭弹窗 清除选中
      handleClose(done) {
        this.reset()
        this.$refs.tree.setCheckedKeys([])
        done()
      },
      // 关闭弹窗 清除选中
      closeDialog() {
        this.reset()
        this.$refs.tree.setCheckedKeys([])
        this.dialogVisible = false
      },
      // 选中的部门数据
      changDeart(data, checked) {
        if (!this.multiple && checked == true) {
          this.$refs.tree.setCheckedNodes([data])
        }
      },
      // 弹窗确认事件
      saveDialog() {
        const deartData = this.multiple === true ? this.$refs.tree.getCheckedNodes() : this.$refs.tree.getCheckedNodes()[0]
        if (this.$refs.tree.getCheckedNodes().length <= 0) {
          $.msg.tips('请先选择部门!')
          return false
        }
        if (!this.multiple) { //单选
          this.deartmentName = deartData.name
          this.deartmentId = deartData.id
        }
        this.$emit('renderDeartment', deartData)
        // this.$refs.tree.setCheckedKeys([])
        this.dialogVisible = false
        this.reset()

      },
      //删除部门数据
      delDeart() {
        const obj = JSON.parse(JSON.stringify(this.deartData))
        this.deartData = {}
        this.deartmentName = ''
        Object.keys(obj).forEach((key) => {
          console.log(key)
          obj[key] = ''
        })
        this.$emit('renderDeartment', obj)
      },
      //回显部门数据方法
      setDeartData(val, canEidt = true) {
        if (this.multiple) {
          this.$nextTick(() => {
            this.$refs.tree.setCheckedKeys([val])
          })
        } else {
          this.deartmentName = val
        }
        this.canEidt = canEidt
      }
    },
  }
})
